package com.chuangyang.fixboxmaster.ui;

import android.os.Bundle;
import android.support.v4.view.ViewCompat;

import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CostItem;
import com.chuangyang.fixboxmaster.bean.OrderPriceItem;
import com.chuangyang.fixboxmaster.bean.Part;
import com.chuangyang.fixboxmaster.ui.fragment.B2BOrderDetailFragment;
import com.chuangyang.fixboxmaster.ui.fragment.BaseFragment;
import com.chuangyang.fixboxmaster.ui.fragment.CashRequestFragment;
import com.chuangyang.fixboxmaster.ui.fragment.home.BaseHomeFragment;
import com.chuangyang.fixboxmaster.ui.fragment.orderprice.CostDetailFragment02;
import com.chuangyang.fixboxmaster.ui.fragment.MySkillFragment;
import com.chuangyang.fixboxmaster.ui.fragment.OrderDetailFragment;
import com.chuangyang.fixboxmaster.ui.fragment.orderprice.CostDetailFragment;
import com.chuangyang.fixboxmaster.ui.fragment.orderprice.DiagnosisFeeFragment;
import com.chuangyang.fixboxmaster.ui.fragment.orderprice.PriceFragment;
import com.chuangyang.fixboxmaster.ui.fragment.orderprice.RpeairTypeFragment;

import java.util.ArrayList;


/**
 * Created by vanshu on 15/3/30 09:37.
 */
public class ModuleActivity extends BaseActivity {
    public static final int ORDER_DETAL_ACTIVITY = 0x000000;
    public static final int CASH_REQUEST_ACTIVITY = 0x000001;
    public static final int APPLIANCE_PRICE_ACTIVITY = 0x000002;
    public static final int COST_DETAIL_ACTIVITY = 0x000003;
    public static final int COST_DIAGNOSIS_FREE_ACTIVITY = 0x000004;
    public static final int REPAIR_TYPE_ACTIVITY = 0x000006;
    public static final int MY_SKILL_ACTIVITY = 0x000007;
    public static final int B2B_ORDER_DETAL_ACTIVITY = 0x000008;
    public static final int COST_DETAIL_ACTIVITY_02 = 0x000017;
    public static final int MY_ORDER_LIST = 0x000018;


    public static final String TYPE = "type";
    public static final String ORDER_ID = "order_id";
    private OrderDetailFragment fragment;
    private B2BOrderDetailFragment b2bFragment;
    public static final String SELECT_PART = "select_part";
    public static final String COST_ITEM = "cost_item";
    public static final String ORDER_PRICE_ITEM = "order_price_item";
    public static final String TRANSCATION_ID = "transactionId";
    public static final String SPECIFICATION_ID = "specificationId";
    public static final String PRODUCT_DATE = "product_date";
    public static final String BRAND_ID = " brand_id";
    public static final String REPAIR_TYPE = " repair_type";
    private BaseFragment mFragment;
    int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module);
        type = getIntent().getIntExtra(TYPE, -1);
        String orderId = getIntent().getStringExtra(ORDER_ID);
        String mTransactionId = getIntent().getStringExtra(TRANSCATION_ID);
        String mSpecificationId = getIntent().getStringExtra(SPECIFICATION_ID);
        String mProductDate = getIntent().getStringExtra(PRODUCT_DATE);
        String mBrandId = getIntent().getStringExtra(BRAND_ID);
        switch (type) {
            case ORDER_DETAL_ACTIVITY:
                setTitle(getResources().getString(R.string.title_activity_order_detail));
                fragment = new OrderDetailFragment();
                Bundle bundle = new Bundle();
                bundle.putString(ORDER_ID, orderId);
                bundle.putString(TRANSCATION_ID,mTransactionId);
                fragment.setArguments(bundle);
                beginTransaction(fragment);
                enableDisableSwipeRefresh(true);
                break;

            case B2B_ORDER_DETAL_ACTIVITY:
                setTitle(getResources().getString(R.string.title_activity_order_detail));
                b2bFragment = new B2BOrderDetailFragment();
                Bundle B2BBundle = new Bundle();
                B2BBundle.putString(ORDER_ID, orderId);
                b2bFragment.setArguments(B2BBundle);
                beginTransaction(b2bFragment);
                enableDisableSwipeRefresh(true);
                break;
            case CASH_REQUEST_ACTIVITY:
                setTitle(getResources().getString(R.string.title_activity_cash_request));
                beginTransaction(CashRequestFragment.newInstance());
                enableDisableSwipeRefresh(false);
                break;
            case COST_DIAGNOSIS_FREE_ACTIVITY:
                setTitle("定价");
                mFragment = DiagnosisFeeFragment.newInstance();
                bundle = getIntent().getExtras();
                bundle.putString(TRANSCATION_ID,mTransactionId);
                bundle.putString(SPECIFICATION_ID,mSpecificationId);
                bundle.putString(ORDER_ID,orderId);
                bundle.putString(BRAND_ID,mBrandId);
                mFragment.setArguments(bundle);
                beginTransaction(mFragment);
                enableDisableSwipeRefresh(false);
                break;
            case APPLIANCE_PRICE_ACTIVITY:
                setTitle("定价");
                mFragment = PriceFragment.newInstance();
                bundle = getIntent().getExtras();
                ArrayList<CostItem> costItems = (ArrayList<CostItem>)bundle.getSerializable(COST_ITEM);
                ArrayList<Part> parts = (ArrayList<Part>)bundle.getSerializable(SELECT_PART);
                CostItem repairType = (CostItem)bundle.getSerializable(ModuleActivity.REPAIR_TYPE);
                bundle = new Bundle();
                bundle.putSerializable(SELECT_PART,parts);
                bundle.putSerializable(COST_ITEM,costItems);
                bundle.putSerializable(ModuleActivity.REPAIR_TYPE,repairType);
                bundle.putString(ORDER_ID,orderId);
                bundle.putString(TRANSCATION_ID,mTransactionId);
                bundle.putString(SPECIFICATION_ID,mSpecificationId);
                bundle.putString(PRODUCT_DATE,mProductDate);
                bundle.putString(BRAND_ID,mBrandId);
                mFragment.setArguments(bundle);
                beginTransaction(mFragment);
                enableDisableSwipeRefresh(false);
                break;
            case COST_DETAIL_ACTIVITY:
                setTitle("定价");
                CostDetailFragment costDetailFragment = CostDetailFragment.newInstance();
                bundle = getIntent().getExtras();
                costItems = (ArrayList<CostItem>)bundle.getSerializable(COST_ITEM);
                ArrayList<OrderPriceItem> items = (ArrayList<OrderPriceItem>)bundle.getSerializable(ORDER_PRICE_ITEM);
                Bundle orderDetailbundle = new Bundle();
                orderDetailbundle.putSerializable(COST_ITEM,costItems);
                orderDetailbundle.putSerializable(ORDER_PRICE_ITEM,items);
                orderDetailbundle.putString(ORDER_ID,orderId);
                orderDetailbundle.putString(TRANSCATION_ID,mTransactionId);
                orderDetailbundle.putString(SPECIFICATION_ID,mSpecificationId);
                orderDetailbundle.putString(PRODUCT_DATE,mProductDate);
                orderDetailbundle.putString(BRAND_ID,mBrandId);
                costDetailFragment.setArguments(orderDetailbundle);
                beginTransaction(costDetailFragment);
                enableDisableSwipeRefresh(false);
                break;

            case REPAIR_TYPE_ACTIVITY:
                setTitle("定价");
                RpeairTypeFragment repairTypeFragment = RpeairTypeFragment.newInstance();
                bundle = getIntent().getExtras();
                String id=bundle.getString("id");
                CostItem  item = (CostItem)bundle.getSerializable(REPAIR_TYPE);
                bundle = new Bundle();
                bundle.putString(TRANSCATION_ID,mTransactionId);
                bundle.putString(SPECIFICATION_ID,mSpecificationId);
                bundle.putString("id",id);
                bundle.putSerializable(REPAIR_TYPE,item);
                repairTypeFragment.setArguments(bundle);
                beginTransaction(repairTypeFragment);
                enableDisableSwipeRefresh(false);
                break;

            case MY_SKILL_ACTIVITY:
                setTitle("我的技能");
                beginTransaction(MySkillFragment.newInstance());
                enableDisableSwipeRefresh(false);
                break;

            case COST_DETAIL_ACTIVITY_02:
                enableDisableSwipeRefresh(false);
                setTitle("费用详情");
                bundle = getIntent().getExtras();
                ArrayList<OrderPriceItem> items2 = (ArrayList<OrderPriceItem>)bundle.getSerializable(ORDER_PRICE_ITEM);
                orderId = bundle.getString(ORDER_ID);
                CostDetailFragment02 coslFragment = CostDetailFragment02.newInstance();
                bundle = new Bundle();
                bundle.putSerializable(ORDER_PRICE_ITEM,items2);
                bundle.putString(ORDER_ID,orderId);
                coslFragment.setArguments(bundle);
                beginTransaction(coslFragment);
                break;

            case MY_ORDER_LIST:
                enableDisableSwipeRefresh(false);
                setTitle("我的订单");
                beginTransaction(BaseHomeFragment.newInstance());
                break;
        }

    }

    private void beginTransaction(BaseFragment fragment) {
        mFragment = fragment;
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }

    @Override
    protected void requestDataRefresh() {
        if(type==ORDER_DETAL_ACTIVITY){
            fragment.updateView();
            onRefreshingStateChanged(false, 1000);
        }else if(type==B2B_ORDER_DETAL_ACTIVITY){
            b2bFragment.updateView();
            onRefreshingStateChanged(false, 1000);
        }
    }

    @Override
    public boolean canSwipeRefreshChildScrollUp() {
        if (mFragment instanceof CostDetailFragment || mFragment instanceof  PriceFragment)
        {
            if(mFragment.getUserVisibleHint() && mFragment.getListView()!=null){
                return ViewCompat.canScrollVertically(mFragment.getListView(), -1);
            }

        }else if(mFragment instanceof  B2BOrderDetailFragment){
            if(((B2BOrderDetailFragment) mFragment).getScrollView()!=null){
                return ViewCompat.canScrollVertically(((B2BOrderDetailFragment) mFragment).getScrollView(), -1);
            }
        }
        return false;
    }
}
